<?php
namespace app\shop\controller;
use app\AdminRest;
use app\member\model\Member;
use app\member\model\StoredOrder;
use think\App;
use app\shop\model\AdminShopOrderRefund as OderRefundModel;
use app\shop\model\AdminShopOrder;
use app\shop\model\IndexCardCount;
use app\shop\model\AdminShopSpePrice;
use app\shop\model\AdminGoods;
use app\shop\model\AdminSellingWater;
use app\shop\model\AdminSellingProfit;
use think\facade\Db;

class AdminShopOrderRefund extends AdminRest
{

    protected $model;
    protected $order_model;
    protected $count_model;
    protected $spe_price_model;
    protected $goods_model;
    protected $selling_water;
    protected $selling_profit;
    public function __construct(App $app) {
        parent::__construct($app);
        $this->model           = new OderRefundModel();
        $this->order_model     = new AdminShopOrder();
        $this->count_model     = new IndexCardCount();
        $this->spe_price_model = new AdminShopSpePrice();
        $this->goods_model     = new AdminGoods();
        $this->selling_water   = new AdminSellingWater();
        $this->selling_profit  = new AdminSellingProfit();
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 18:50
     * @return \think\Response
     * descption: 退款订单列表
     */
    public function orderRefundList(){

//        file_get_contents('https://new.likua.cn/');
        $input = $this->_input;
        $dis[] = ['a.uniacid','=',$this->_uniacid];
        if(!empty($input['order_status'])){
            $dis[] = ['a.status','=',$input['order_status']];
        }
        if(!empty($input['transaction_id'])){
            $dis[] = ['b.transaction_id','like',"%".$input['transaction_id']."%"];
        }
        $data = $this->model->refundList($dis,$input['limit']);
        return $this->success($data);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 18:51
     * @return \think\Response
     * descption: 退款订单详情
     */
    public function orderRefundInfo(){
        $dis  = ['a.id'=>$this->_input['id']];
        $res  = $this->model->refundInfo($dis);
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 18:50
     * @return \think\Response
     * descption:同意退款
     */
    public function orderRefund(){

        $input = $this->_input;

        $refund_price = !empty($input['refund_price'])?$input['refund_price']:0;

        $res   = $this->model->orderRefundApi($input['id'],$refund_price,$this->app,$this->payConfig());

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }
        return $this->success($res);
    }

    /**
     * User: chenniang
     * Date: 2019-09-09 18:46
     * @return void
     * descption:拒绝退款
     */
    public function cancelRefund(){
        $input = $this->_input;
        $item  = $this->model->refundInfo(['a.id' => $input['id']]);
        if (empty($item)) {
            return $this->error('not find refundorder');
        }
        if (empty($item['out_trade_no'])) {
            return $this->error('not find order');
        }
        if ( $item[ 'status' ] != 1 || $item[ 'refund_status' ] != 1 ) {
            return $this->error('status is error');
        }
        $this->order_model->orderUpdate(['id' => $item[ 'order_id' ]],['refund_status' => 3]);
        $this->model->refundUpdate(['id'=> $input['id']],['status'=>3]);
        $insertData = array(
            'user_id' => $item['user_id'],
            'to_uid'  => $item['to_uid'],
            'sign'    => 'view',
            'type'    => 23,
            'uniacid' => $this->_uniacid,
            'target'  => $item['order_id']
        );
        $insert_id = $this->count_model->countAdd($insertData);
        $push_data = array(
            'action'  => 'longbingSendWxServiceNotice',
            'event'   => 'longbingSendWxServiceNotice',
            'count_id'=> $insert_id
        );
        publisher(json_encode($push_data ,true));
        return $this->success(true);
    }







}
